﻿/// <reference path="../../../scripts/typings/knockout/knockout.d.ts" />
/// <reference path="../../../scripts/typings/winrt/winrt.d.ts" />
import ko = require("knockout");
import Editor = require("../../Editor");

interface IFoundProjectFile {
    filename: string;
    parentFolder: Windows.Storage.StorageFolder;
    storageFile: Windows.Storage.StorageFile;
}

class SaveProjectDialog {
    public constructor(public params: { editor: Editor }) {
        this.editor = params.editor;
        this.projectsFound = ko.observableArray([]);
        this.selectedFile = ko.observable(null);
        this.folder = ko.observable(null);
        this.newProjectName = ko.observable("");
        this.folderPath = ko.pureComputed(() => {
            if (this.folder()) {
                return this.folder().path;
            }
            return "";
        });
        this.creatingNew = ko.pureComputed(() => {
            return this.folder() && !this.selectedFile();
        });
        this.processing = ko.observable(false);
        this.folderButtonText = ko.pureComputed(() => {
            return !this.folder() ? "Select folder" : "Choose another";
        });
    }

    public selectFolder = () => {
        var picker = new Windows.Storage.Pickers.FolderPicker();
        picker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
        picker.fileTypeFilter.replaceAll([".editone"]);
        picker.pickSingleFolderAsync().then((folder) => {
            if (!folder) {
                return;
            }
            this.projectsFound([]);
            this.folder(folder);

            var query = folder.createFileQueryWithOptions(
                new Windows.Storage.Search.QueryOptions(
                    Windows.Storage.Search.CommonFileQuery.orderByName,
                    <any>[".editone"]
                    )
                );

            query.getFilesAsync().then((files) => {
                var projs = [];

                files.forEach((value, index, array) => {
                    projs.push({
                        filename: value.name,
                        parentFolder: folder,
                        storageFile: value
                    });
                });
                this.projectsFound(projs);
            });
        });
    }

    save = () => {
        if (!this.folder()) {
            return;
        }

        var fname = this.creatingNew() ? this.newProjectName() : this.selectedFile().filename;
        if (!(/\.editone$/.test(fname))) {
            fname += '.editone';
        }

        this.processing(true);
        this.editor.project().project.save(this.folder(), fname).then(() => {
            this.processing(false);
            this.editor.showTracksTab();
        });
    }

    private projectsFound: KnockoutObservable<IFoundProjectFile[]>;
    private selectedFile: KnockoutObservable<IFoundProjectFile>;
    folder: KnockoutObservable<Windows.Storage.StorageFolder>;
    folderPath: KnockoutComputed<string>;
    editor: Editor;
    creatingNew: KnockoutComputed<boolean>;
    newProjectName: KnockoutObservable<string>;
    processing: KnockoutObservable<boolean>;
    folderButtonText: KnockoutComputed<string>;
}

export = SaveProjectDialog;